{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-10T07:20:21.456266Z",
     "start_time": "2018-09-10T07:20:21.182468Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import torch\n",
    "import time\n",
    "import pickle\n",
    "import matplotlib.pyplot as plt\n",
    "from collections import OrderedDict\n",
    "from torch.autograd import Variable\n",
    "from pathlib import Path\n",
    "from tqdm import tqdm\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-10T07:20:21.808633Z",
     "start_time": "2018-09-10T07:20:21.778666Z"
    }
   },
   "outputs": [],
   "source": [
    "pix2pixhd_dir = Path('../src/pix2pixHD/')\n",
    "\n",
    "import sys\n",
    "sys.path.append(str(pix2pixhd_dir))\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-10T07:20:22.295862Z",
     "start_time": "2018-09-10T07:20:22.247818Z"
    }
   },
   "outputs": [],
   "source": [
    "from data.data_loader import CreateDataLoader\n",
    "from models.models import create_model\n",
    "import util.util as util\n",
    "from util.visualizer import Visualizer\n",
    "from util import html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-10T07:20:23.551660Z",
     "start_time": "2018-09-10T07:20:23.541934Z"
    }
   },
   "outputs": [],
   "source": [
    "with open('../data/test_opt.pkl', mode='rb') as f:\n",
    "    opt = pickle.load(f)\n",
    "    \n",
    "iter_path = os.path.join(opt.checkpoints_dir, opt.name, 'iter.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-10T07:20:24.277739Z",
     "start_time": "2018-09-10T07:20:24.114263Z"
    }
   },
   "outputs": [],
   "source": [
    "data_loader = CreateDataLoader(opt)\n",
    "dataset = data_loader.load_data()\n",
    "visualizer = Visualizer(opt)\n",
    "# create website\n",
    "web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch))\n",
    "webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-10T07:20:29.388782Z",
     "start_time": "2018-09-10T07:20:26.586791Z"
    }
   },
   "outputs": [],
   "source": [
    "model = create_model(opt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-10T07:20:34.479096Z",
     "start_time": "2018-09-10T07:20:30.872933Z"
    }
   },
   "outputs": [],
   "source": [
    "for data in tqdm(dataset):\n",
    "    minibatch = 1 \n",
    "    generated = model.inference(data['label'], data['inst'])\n",
    "        \n",
    "    visuals = OrderedDict([('input_label', util.tensor2label(data['label'][0], opt.label_nc)),\n",
    "                           ('synthesized_image', util.tensor2im(generated.data[0]))])\n",
    "    img_path = data['path']\n",
    "    visualizer.save_images(webpage, visuals, img_path)\n",
    "webpage.save()\n",
    "torch.cuda.empty_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3.6",
   "language": "python",
   "name": "python3.6"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
